home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.std.c
- Path: phcoms4.seri.philips.nl!panther!baynes
- From: baynes@ukpsshp1.serigate.philips.nl (Stephen Baynes)
- Subject: Re: Undefined result vs. int's holding undefined values.
- Sender: news@ukpsshp1.serigate.philips.nl (account for localnews)
- Message-ID: <DKyIx1.1qr@ukpsshp1.serigate.philips.nl>
- Date: Wed, 10 Jan 1996 08:43:49 GMT
- References: <4ck70b$rd7@news.informix.com> <4ckms5$rd7@news.informix.com> <4cmg0s$1mb@der.twinsun.com> <oZA8wQ9ytpjN084yn@csn.net> <4cs460$d6e@news.informix.com>
- Organization: Philips Semiconductors, Southampton, UK
- X-Newsreader: TIN [version 1.2 PL2]
-
- Daniel Wood (dwood@informix.com) wrote:
- : thads@csn.net (Thad Smith) wrote:
- : >In article <4cmg0s$1mb@der.twinsun.com>,
- : Do any machines exist which actually explode when you add two number together
- : such that the result would exceed MAXINT? :-) Get pratical!
-
- VAXes
-
- If you set the apropriate bit in the process status word then integer overflow
- causes a trap. I think that by default this is not enabled, and it could be
- difficult to use in practice (it traps on signed overflow, but how do you
- distinguish this from valid unsigned arithmetic? The compiler would have
- to generate instructions to switch this on and off. I can't recall if VAX
- C compiler offers this option, however I recall it is the default for VAX
- pascal to enable overflow checks.
-
- It is true most compilers don't offer traping overflows and those that do
- tend not to do it by default. I think that this is partly because there is so
- much C code out there that erroneously overflows but manages to keep working
- that most people don't bother with it. However not bothering with it means
- that more accidental overflows are written into the code. Also it can add
- alot of execution overhead, even when there is hardware support there may
- be some.
-
-
-
- The question started up over a bit of code that was written to check if
- overflow would occur. It has always seemed a weekness of C that there is
- no defined way to do this. As in the example interpretors are one case. However
- there are may applications where it would be better to crash than produce
- wrong answeres that will cause trouble latter. Even better would be a simple
- way for applications to check if certain arithmetic operations have worked
- safely. Can anyone make a recomendation for a good language extension to add
- this? Because there is so much legacy code out there it must be selective and
- must not add overhead to unchecked operations. It may be one needs three levels
- of operation:
- 1: Expected to overflow during normal operation. Overflow is safe and
- should not be reported. [This is the backwards compatible
- case for unsigned and may need to be for signed.]
- 2: Overflow is an error, if practicable it should be caught and reported
- by some implementation specified means. [This is the strict backwards
- compatible case for signed. It probably should be the default for
- most code.]
- 3: Overflow is possible. It _must_ be caught and handled by the application.
- [For example it could set errno or raise a signal - but it must be
- defined.]
-
- How do we handle these cases in standard C? What changes can we make to the
- standard to make it possible?
-
-
- --
- Stephen Baynes baynes@mulsoc2.serigate.philips.nl
- Philips Semiconductors Ltd
- Southampton My views are my own.
- United Kingdom
-